package cq.zhcqk.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebSettings;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dou361.dialogui.DialogUIUtils;
import com.dou361.dialogui.bean.BuildBean;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
//import com.mrw.wzmrecyclerview.Divider.BaseItemDecoration;
//import com.mrw.wzmrecyclerview.HeaderAndFooter.OnItemClickListener;
//import com.mrw.wzmrecyclerview.HeaderAndFooter.OnItemLongClickListener;
//import com.mrw.wzmrecyclerview.PullToLoad.OnLoadListener;
//import com.mrw.wzmrecyclerview.PullToLoad.PullToLoadRecyclerView;
//import com.mrw.wzmrecyclerview.PullToRefresh.OnRefreshListener;
//import com.mrw.wzmrecyclerview.SimpleAdapter.SimpleAdapter;
//import com.mrw.wzmrecyclerview.SimpleAdapter.ViewHolder;
import com.jimi_wu.ptlrecyclerview.Divider.BaseItemDecoration;
import com.jimi_wu.ptlrecyclerview.HeaderAndFooter.OnItemClickListener;
import com.jimi_wu.ptlrecyclerview.HeaderAndFooter.OnItemLongClickListener;
import com.jimi_wu.ptlrecyclerview.PullToLoad.OnLoadListener;
import com.jimi_wu.ptlrecyclerview.PullToLoad.PullToLoadRecyclerView;
import com.jimi_wu.ptlrecyclerview.PullToRefresh.OnRefreshListener;
import com.jimi_wu.ptlrecyclerview.SimpleAdapter.SimpleAdapter;
import com.jimi_wu.ptlrecyclerview.SimpleAdapter.ViewHolder;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tencent.smtt.sdk.DownloadListener;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.zzhoujay.richtext.RichText;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.app;
import cq.zhcqk.util.AsyncHttpClientUtils;
import cq.zhcqk.util.CheckLogin;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.GsonUtil;
import cq.zhcqk.util.ImageLoaderHelper;
import cq.zhcqk.util.JsUtil;
import cq.zhcqk.util.StringUtil;
import cq.zhcqk.util.WebViewHeader;
import cq.zhcqk.util.x5.X5WebView;

/**
 * Created by zhangle on 2017/11/26.
 */
public class ListDetailActivity extends Activity implements View.OnClickListener {
    private Context context;
    private AsyncHttpClient httpClient;
    private String cid;
    private TextView content_text;
    private EditText pinglun_text;
    private ImageView shouchang;
    private ImageView pinglun_commit;
    private ImageView pinglun_1;
    private TextView slug;
    private TextView title;
    private TextView item_num;
    private TextView item_time;
    private RichText richText;
    private BuildBean buildBean;
    private PullToLoadRecyclerView rcv;
    private View headerView;
    private ArrayList<Map<String, Object>> list;
    private int slug_num = 0;
    private Map<String, Object> mapData;
    private LinearLayout detail;
//    private WebView webTest;

    private LinearLayout rightLay;
    private TextView pinglunSub;

    private X5WebView tc_webview;
    private String tt;
    private String url;
    private String categories;

    private String isCollect = "false";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_detail);
        context = this;
        httpClient = AsyncHttpClientUtils.getInstance().getAsyncHttpClient();
        cid = getIntent().getStringExtra("cid");
        tt = getIntent().getStringExtra("title");
        url = getIntent().getStringExtra("url");
        categories = getIntent().getStringExtra("categories");
        initView();
        initBack();
        initRecyclerView();
//        getDetail();
//        getList();
        if(app.isLogin){
            iscollect();
        }
    }

    private void initView(){
        RichText.initCacheDir(this);
        buildBean = DialogUIUtils.showLoading(this, "加载中...", true, true, true, false);
        pinglun_text = (EditText) findViewById(R.id.pinglun_text);
        rightLay = (LinearLayout) findViewById(R.id.right_lay);
        pinglunSub = (TextView) findViewById(R.id.pinglun_sub);
        shouchang = (ImageView) findViewById(R.id.shouchang);
        pinglun_commit = (ImageView) findViewById(R.id.pinglun_commit);
        pinglun_1 = (ImageView) findViewById(R.id.pinglun_1);
        headerView = LayoutInflater.from(context).inflate(R.layout.news_list_detail_header,null);
        detail = (LinearLayout) headerView.findViewById(R.id.detail_lay);
        content_text = (TextView) headerView.findViewById(R.id.content_text);
//        webTest = (WebView)headerView.findViewById(R.id.webTest);
        slug = (TextView) headerView.findViewById(R.id.slug);
        title = (TextView) headerView.findViewById(R.id.title);
        item_num = (TextView) headerView.findViewById(R.id.num);
        item_time = (TextView) headerView.findViewById(R.id.time);

        item_num.setOnClickListener(this);
        shouchang.setOnClickListener(this);
        slug.setOnClickListener(this);
        pinglun_commit.setOnClickListener(this);
        pinglun_1.setOnClickListener(this);
        pinglunSub.setOnClickListener(this);

        pinglun_text.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    rightLay.setVisibility(View.GONE);
                    pinglunSub.setVisibility(View.VISIBLE);
                }else{
                    rightLay.setVisibility(View.VISIBLE);
                    pinglunSub.setVisibility(View.GONE);
                }
            }
        });

        tc_webview = (X5WebView)headerView. findViewById(R.id.tc_webview);
        if(StringUtil.isNull(url)){
            WebViewHeader.setHeaderAndLoadX5(tc_webview,Common.URL+"/blog/open/post/"+cid);
        }else{
            WebViewHeader.setHeaderAndLoadX5(tc_webview,url);
        }
        show();
    }
    private void initBack(){
        ImageView back = (ImageView) findViewById(R.id.header_back);
        TextView title = (TextView) findViewById(R.id.header_text);
        title.setText("和美磁器口");
        back.setOnClickListener(this);
    }

    public void show(){
        if(tc_webview != null)
        {
            com.tencent.smtt.sdk.WebSettings settings = tc_webview.getSettings();
            tc_webview.setDownloadListener(new MyWebViewDownLoadListener());
            settings.setJavaScriptEnabled(true);
            settings.setBuiltInZoomControls(true);
            settings.setLoadWithOverviewMode(true);
            settings.setUseWideViewPort(true);
            settings.setDatabaseEnabled(true);
            settings.setBuiltInZoomControls(false);
            settings.setSupportZoom(false);
            settings.setDisplayZoomControls(false);

            tc_webview.addJavascriptInterface(new JavascriptInterface(context), "imagelistner");
            tc_webview.setWebViewClient(new WebViewClient()
            {
                @Override
                public void onPageFinished(WebView webView, String s) {
                    super.onPageFinished(webView, s);
                    DialogUIUtils.dismiss(buildBean);
                    JsUtil.addX5ImageClickListner(tc_webview);
                    getList();
                    rcv.setVisibility(View.VISIBLE);
                    storenum();
                    slug.setVisibility(View.VISIBLE);
                }

                @Override
                public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
                    super.onPageStarted(webView, s, bitmap);
                    buildBean.show();
                }
            });
        }
    }

    public class JavascriptInterface {

        private Context context;

        public JavascriptInterface(Context context) {
            this.context = context;
        }
        @android.webkit.JavascriptInterface
        public void openImage(String img) {
            Intent intent = new Intent();
            intent.putExtra("image", img);
            intent.setClass(context, ShowWebImageActivity.class);
            context.startActivity(intent);
            System.out.println(img);
        }
    }

    private void initRecyclerView(){
        rcv = (PullToLoadRecyclerView)findViewById(R.id.rc_detail);

        rcv.setLayoutManager(new GridLayoutManager(context,1));
        rcv.setRefreshEnable(false);
        rcv.setLoadEnable(false);
//        设置适配器，封装后的适配器只需要实现一个函数
        list = new ArrayList<>();
        rcv.setAdapter(new SimpleAdapter<Map<String, Object>>(context, list, R.layout.item_list_recycler) {
            @Override
            protected void onBindViewHolder(ViewHolder holder, Map<String, Object> data) {
                String allowPing = data.get("allowPing")+"";
                if("0.0".equals(allowPing)){
                    holder.<ImageView>getView(R.id.item_header_img).setVisibility(View.VISIBLE);
                    holder.<ImageView>getView(R.id.item_content_img).setVisibility(View.GONE);
                    ImageLoader.getInstance().displayImage(StringUtil.checkImgeUrl(data.get("categories")+""), holder.<ImageView>getView(R.id.item_header_img), ImageLoaderHelper.getOptionsDf());
                }else if("2.0".equals(allowPing)){
                    holder.<ImageView>getView(R.id.item_header_img).setVisibility(View.GONE);
                    holder.<ImageView>getView(R.id.item_content_img).setVisibility(View.GONE);
                }else if("3.0".equals(allowPing)){
                    holder.<ImageView>getView(R.id.item_header_img).setVisibility(View.GONE);
                    holder.<ImageView>getView(R.id.item_content_img).setVisibility(View.VISIBLE);
                    ImageLoader.getInstance().displayImage(StringUtil.checkImgeUrl(data.get("categories")+""), holder.<ImageView>getView(R.id.item_content_img), ImageLoaderHelper.getOptionsDf());
                }
                holder.<TextView>getView(R.id.item_title).setText(""+data.get("title"));
                holder.<TextView>getView(R.id.item_num).setText(""+ StringUtil.stringToInt(data.get("hits")+""));
                holder.<TextView>getView(R.id.item_time).setText(""+data.get("time"));
            }
        });

        rcv.addHeaderView(headerView);
//        设置刷新监听
        rcv.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onStartRefreshing() {
            }
        });
//        设置加载监听
        rcv.setOnLoadListener(new OnLoadListener() {
            @Override
            public void onStartLoading(int skip) {
            }
        });
//        设置分割线
        rcv.addItemDecoration(new BaseItemDecoration(context,R.color.gray_bg));

        rcv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void OnItemClick(int position) {
                Intent intent = new Intent(context, ListDetailSampleActivity.class);
                intent.putExtra("cid",(int) Double.parseDouble(String.valueOf(list.get(position).get("cid")))+"");
                intent.putExtra("url",String.valueOf(list.get(position).get("url"))+"");
                startActivity(intent);
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
            }
        });
        rcv.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(int position) {
                return true;
            }
        });
    }

    private String relaceUrl(String content){
        String info = content.replace("<img src=\"/","<img src=\""+Common.URL+"/");
        return info;
    }

//    private void getDetail(){
//        WebSettings settings = webTest.getSettings();
//        settings.setJavaScriptEnabled(true);
//        settings.setBuiltInZoomControls(true);
//        settings.setLoadWithOverviewMode(true);
//        settings.setUseWideViewPort(true);
//        settings.setDatabaseEnabled(true);
//
//        settings.setDomStorageEnabled(true);
//        webTest.setWebChromeClient(new WebChromeClient());
//        settings.setPluginState(WebSettings.PluginState.ON);
//        settings.setAllowFileAccess(true); // 允许访问文件
//        settings.setCacheMode(WebSettings.LOAD_NO_CACHE); // 不加载缓存内容
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
//        }
////        webTest.addJavascriptInterface(new MyJavaScript(), "JsUtils");
//
////                        webTest.loadUrl(Common.URL+"/blog/open/post/"+cid);
//        WebViewHeader.setHeaderAndLoad(webTest,Common.URL+"/blog/open/post/"+cid);
//
////        RequestParams params = new RequestParams();
////        params.add("cid",cid);
////        httpClient.post(Common.URL+Common.NEWS_DETAIL,params,new JsonHttpResponseHandler(){
////            @Override
////            public void onSuccess(int statusCode, Header[] headers,
////                                  JSONObject response) {
////                super.onSuccess(statusCode, headers, response);
////                System.out.println("onSuccess："+response);
////                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
////                if(map != null && map.size()>0){
////                    mapData = (Map<String, Object>) map.get("data");
////                    if(mapData != null && mapData.size()>0){
////                        richText = RichText.from(relaceUrl(mapData.get("content")+"")).into(content_text);
////                        slug_num = StringUtil.stringToInt(mapData.get("slug")+"");
////                        title.setText(mapData.get("title")+"");
////                        item_num.setText(StringUtil.stringToInt(mapData.get("hits")+"")+"");
////                        item_time.setText(mapData.get("time")+"");
////                        setSlug();
////                        WebSettings settings = webTest.getSettings();
////                        settings.setJavaScriptEnabled(true);
////                        settings.setBuiltInZoomControls(true);
////                        settings.setLoadWithOverviewMode(true);
////                        settings.setUseWideViewPort(true);
////                        settings.setDatabaseEnabled(true);
//////                        webTest.loadUrl(Common.URL+"/blog/open/post/"+cid);
////                        WebViewHeader.setHeaderAndLoad(webTest,Common.URL+"/blog/open/post/"+cid);
////                        detail.setVisibility(View.VISIBLE);
////
////                    }
////                }
////            }
////
////            @Override
////            public void onFailure(int statusCode, Header[] headers,
////                                  Throwable throwable, JSONObject errorResponse) {
////                super.onFailure(statusCode, headers, throwable,
////                        errorResponse);
////                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
////            }
////
////            @Override
////            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
////                super.onFailure(statusCode, headers, responseString, throwable);
////                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
////            }
////
////            @Override
////            public void onFinish() {
////                super.onFinish();
////                DialogUIUtils.dismiss(buildBean);
////            }
////        });
//    }

    private void getList(){
//        buildBean.show();
        RequestParams params = new RequestParams();
        httpClient.post(Common.URL+Common.NEWS_TOPS,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("response:"+response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                ArrayList<Map<String, Object>> listNew = null;
                listNew = (ArrayList<Map<String, Object>>)map.get("data");
                if(listNew == null || listNew.size()==0){
                    Toast.makeText(context,"没有更多",Toast.LENGTH_SHORT).show();
                }else{
                    list.addAll(listNew);
                }
                rcv.completeRefresh();
                rcv.completeLoad(1);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }

    private void pinglun(String discusscontent){
        RequestParams params = new RequestParams();
        params.add("dataid",cid);
        params.add("discusscontent",discusscontent);
        httpClient.post(Common.URL+Common.NEWS_DIC,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Toast.makeText(context,"发布评论成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("onFailure："+errorResponse);
                CheckLogin.checkLogin(context,errorResponse+"");
//                Toast.makeText(context,"发布评论失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Toast.makeText(context,"发布评论失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }

    private void shouChang(){
        RequestParams params = new RequestParams();
        params.add("cid",cid);
        httpClient.post(Common.URL+Common.NEWS_DCT,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
//                slug_num++;
//                setSlug();
                Toast.makeText(context,map.get("msg")+"",Toast.LENGTH_SHORT).show();
                if("true".equals(isCollect)){
                    slug_num--;
                    setSlug();
                    isCollect = "false";
                    shouchang.setImageResource(R.mipmap.shouchang_3);
                }else{
                    slug_num++;
                    setSlug();
                    isCollect = "true";
                    shouchang.setImageResource(R.mipmap.sc_red);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("onFailure："+errorResponse);
                CheckLogin.checkLogin(context,errorResponse+"");
//                Toast.makeText(context,"发布评论失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Toast.makeText(context,"收藏失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }

    private void iscollect(){
        RequestParams params = new RequestParams();
        params.add("cid",cid);
        httpClient.post(Common.URL+Common.ISCOLLECT,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                isCollect = map.get("data")+"";
                if("true".equals(map.get("data")+"") ){
                    shouchang.setImageResource(R.mipmap.sc_red);
                }else{
                    shouchang.setImageResource(R.mipmap.shouchang_3);
                }
//                slug_num++;
//                setSlug();
//                Toast.makeText(context,"收藏成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("onFailure："+errorResponse);
                CheckLogin.checkLogin(context,errorResponse+"");
//                Toast.makeText(context,"发布评论失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
//                Toast.makeText(context,"收藏失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
//                DialogUIUtils.dismiss(buildBean);
            }
        });
    }

    private void storenum(){
        RequestParams params = new RequestParams();
        params.add("cid",cid);
        httpClient.post(Common.URL+Common.STORENUM,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("storenum:"+response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                if(map.get("data") != null){
                    slug_num = StringUtil.stringToInt(map.get("data")+"");
                }else{
                    slug_num = 0;
                }
                setSlug();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("onFailure："+errorResponse);
                CheckLogin.checkLogin(context,errorResponse+"");
//                Toast.makeText(context,"发布评论失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
//                Toast.makeText(context,"收藏失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }

    public void setSlug(){
        slug.setText(slug_num+"人收藏");
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()){
            case R.id.header_back:
                if (tc_webview.canGoBack())
                {
                    tc_webview.goBack(); //goBack()表示返回WebView的上一页面
                }else
                {
                    finish();
                }
                break;
            case R.id.pinglun_1:
                intent = new Intent(context,DicListActivity.class);
                intent.putExtra("cid",cid+"");
                startActivity(intent);
                break;
            case R.id.pinglun_commit:
//                if(!app.isLogin){
//                    Toast.makeText(context,"请先登录",Toast.LENGTH_SHORT).show();
//                    context.startActivity(new Intent(context, LoginActivity.class));
//                    overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
//                    return;
//                }
//                String discusscontent = pinglun_text.getText()+"";
//                if("".equals(discusscontent) || discusscontent == null){
//                    Toast.makeText(context,"请输入评论内容",Toast.LENGTH_SHORT).show();
//                    return;
//                }
//                buildBean.show();
//                pinglun(discusscontent);

//                Intent textIntent = new Intent(Intent.ACTION_SEND);
//                textIntent.setType("text/plain");
//                textIntent.putExtra(Intent.EXTRA_TEXT,Common.URL+"/blog/open/post/"+cid);
//                startActivity(Intent.createChooser(textIntent, "分享"));

                UMWeb web = new UMWeb(Common.URL+"/blog/open/post/"+cid);
                web.setTitle("和美磁器口");//标题
                web.setThumb(new UMImage(context,Common.URL+categories));  //缩略图
                web.setDescription(tt);//描述

                new ShareAction(ListDetailActivity.this)
                        .setDisplayList(SHARE_MEDIA.QQ,SHARE_MEDIA.QZONE,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE)
                        .withMedia(web)
                        .open();
//                new ShareAction(ListDetailActivity.this).withText("hello").setDisplayList(SHARE_MEDIA.QQ,SHARE_MEDIA.QZONE,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.WEIXIN_CIRCLE)
//                        .setCallback(shareListener).open();
                break;
            case R.id.pinglun_sub:
                if(!app.isLogin){
                    Toast.makeText(context,"请先登录",Toast.LENGTH_SHORT).show();
                    context.startActivity(new Intent(context, LoginActivity.class));
                    overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                    return;
                }
                String discusscontent = pinglun_text.getText()+"";
                if("".equals(discusscontent) || discusscontent == null){
                    Toast.makeText(context,"请输入评论内容",Toast.LENGTH_SHORT).show();
                    return;
                }
                buildBean.show();
                pinglun(discusscontent);
                break;
            case R.id.shouchang:
                if(!app.isLogin){
                    Toast.makeText(context,"请先登录",Toast.LENGTH_SHORT).show();
                    context.startActivity(new Intent(context, LoginActivity.class));
                    overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                    return;
                }
                buildBean.show();
                shouChang();
;
                break;
            case R.id.slug:
                if(!app.isLogin){
                    Toast.makeText(context,"请先登录",Toast.LENGTH_SHORT).show();
                    context.startActivity(new Intent(context, LoginActivity.class));
                    overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
                    return;
                }
                buildBean.show();
                shouChang();
                break;
            case R.id.num:
                intent = new Intent(context,DicListActivity.class);
                intent.putExtra("cid",StringUtil.stringToInt(mapData.get("cid")+"")+"");
                startActivity(intent);
                break;
        }
    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(ListDetailActivity.this,"成功了",Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(ListDetailActivity.this,"失败"+t.getMessage(),Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(ListDetailActivity.this,"取消了",Toast.LENGTH_LONG).show();

        }
    };

    private class MyWebViewDownLoadListener implements DownloadListener {

        @Override
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,long contentLength) {
            Uri uri = Uri.parse(url);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
//        webTest.setVisibility(View.GONE);
//        webTest.removeAllViews();
//        webTest.destroy();
        if (this.tc_webview != null) {
            tc_webview.setVisibility(View.GONE);
            tc_webview.removeAllViews();
            tc_webview.destroy();
        }
        RichText.recycle();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) ) {
            if (tc_webview.canGoBack())
            {
                tc_webview.goBack(); //goBack()表示返回WebView的上一页面
                return true;
            }else
            {
                finish();
                return true;
            }

        }
        return false;
    }
}
